package com.scalyze.apps.socialanyltics.datastore;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.scalyze.apps.socialanyltics.datastore.persistence.DataHandler;
import com.scalyze.apps.socialanyltics.datastore.persistence.HBaseDataHandler;


public class JSONService
{

    /** The handler. */
    private DataHandler handler;
    private boolean isConnected;

    public void parseMessage(String message, String contactNode, String port,String zookeeperHost, String trendName)
    {
        try
        {
            JSONObject dataObj = new JSONObject(message);
//            System.out.println(dataObj.getString("id_str"));
            connect(contactNode, port,zookeeperHost);
            handler.writeData(dataObj, trendName);
        }
        catch (JSONException e)
        {
            System.out.println(e.getMessage());
//            throw new RuntimeException(e.getMessage());
        }
        catch (IOException e)
        {
            throw new RuntimeException(e.getMessage());
        }
    }


    public void connect(String contactNode, String port, String zookeeperHost)
    {
        if (!isConnected)
        {
            handler = new HBaseDataHandler(contactNode, port,zookeeperHost);
            isConnected = true;
        }
    }
}
